const app = getApp()
var CONFIG = require("../../config/config.js");
Page({
  data: {
    api_host: CONFIG.API_PIC,
    list: [],
    code: 0,
    msg: '',  
    pages: 0,
    page: 0,
    count: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user_id = app.globalData.uid;
    this.getMyComplainsInit(user_id);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var user_id = app.globalData.uid;
    this.getMyComplainsInit(user_id);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var user_id = app.globalData.uid;
    this.getMyComplainsMore(user_id, this.data.page);
  },
  
  // 用户点击右上角分享

  onShareAppMessage: function () {
    return {
      title: CONFIG.SHARETITLE,
      path: CONFIG.PATHLOGIN
    }
  },
  
  getMyComplainsInit: function (user_id) {
    wx.showLoading({
      title: '加载中~',
      mask: true
    })
    let that = this;
    wx.request({
      url: CONFIG.API_HOST + 'crm/my/getMyComplains',
      data: {
        user_id: user_id
      },
      header: {
        'ContentType': 'application/json'
      },
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.code == 1) {
          that.setData({
            code: res.data.code,
            msg: res.data.msg,
            pages: res.data.pages,
            page: res.data.page,
            list: res.data.list
          })
        } else {
          that.setData({
            code: res.data.code,
            msg: res.data.msg,
            pages: res.data.pages,
            page: res.data.page,
            list: that.data.list
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();
        wx.showModal({
          title: CONFIG.MODEL_TITLE,
          showCancel: false,
          content: CONFIG.REQUEST_FAIL,
          confirmText: '点击重试',
          success: function (res) {
            wx.navigateBack();
          }
        })
      }
    })
  },

  getMyComplainsMore: function (user_id, page) {
    wx.showLoading({
      title: '加载中~',
      mask: true
    })
    let that = this;
    wx.request({
      url: CONFIG.API_HOST + 'crm/my/getMyComplains',
      data: {
        user_id: user_id,
        page: page
      },
      header: {
        'ContentType': 'application/json'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 1) {
          var newslist = res.data.list;
          var lists = that.data.list.concat(newslist);

          that.setData({
            code: res.data.code,
            msg: res.data.msg,
            pages: res.data.pages,
            page: res.data.page,
            list: lists
          })
        } else {
          that.setData({
            code: res.data.code,
            msg: res.data.msg,
            pages: res.data.pages,
            page: res.data.page,
            list: that.data.list
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();
        wx.showModal({
          title: CONFIG.MODEL_TITLE,
          showCancel: false,
          content: '网络请求失败',
          confirmText: '点击重试',
          success: function (res) {
            wx.navigateBack();
          }
        })
      }
    })
  }
})